//
//  NativePickerChannel.m
//  kq_flutter_widgets
//
//  Created by wtb on 2023/4/12.
//

#import "NativePickerChannel.h"
#import "KqPhotoAlert.h"
#import "KqImageTool.h"
#import "KqTool.h"

@implementation NativePickerChannel
+(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar{
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"flutter.keqiang.com/filePicker" binaryMessenger:[registrar messenger]];
    NativePickerChannel *picker = [[NativePickerChannel alloc] init];
    [registrar addMethodCallDelegate:picker channel:channel];
}

-(void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    if ([@"takePhoto" isEqualToString:call.method]){
        self.result = result;
        //拍照
        [[KqPhotoAlert sharedInstance] handleSelectCameraWithTarget:self];
    }else if ([@"pickImages" isEqualToString:call.method] || [@"pickFiles" isEqualToString:call.method]){
        self.result = result;
        //选择照片
        int count = [call.arguments[@"maxCount"] intValue];
        [[KqPhotoAlert sharedInstance] handleSelectLibraryWithTarget:self maxCount:count];
    }
}

#pragma -make UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image) {
        [self getImagesInfo:@[image] isTakePhoto:YES];
    }
}

-(void)photoAlert:(KqPhotoAlert *__nullable)photoAlert images:(NSArray <UIImage *> *__nullable)images{
    [self getImagesInfo:images isTakePhoto:NO];
}

-(void)getImagesInfo:(NSArray *)images isTakePhoto:(BOOL)isTakePhoto{
    NSMutableArray *imageList = [NSMutableArray array];
    for (int i = 0; i < images.count; i++) {
        UIImage *image = images[i];
        NSData *imgData = [KqImageTool kqQualityCompressImage:image];
        if (imgData) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSUInteger fileSize = imgData.length;
            [dic setValue:[NSNumber numberWithInteger:fileSize] forKey:@"fileLength"];
            NSString *imgType = [KqImageTool kqImageTypeWithData:imgData];
            NSString *mime = [NSString stringWithFormat:@"image/%@",imgType];
            [dic setValue:mime forKey:@"mime"];
            [dic setValue:imgType forKey:@"suffix"];
            NSString *fileName = [NSString stringWithFormat:@"%@_%d.%@",[KqTool getNowTimeTimestamp],i,imgType];
            
            NSString *filePath = [KqTool getTmpImagePath:fileName folderName:@"kq"];
            [imgData writeToFile:filePath atomically:YES];
            [dic setValue:filePath forKey:@"filePath"];
            [dic setValue:fileName forKey:@"fileName"];
            [imageList addObject:dic];
        }
    }
    if (self.result && imageList.count > 0) {
        if (isTakePhoto) {
            self.result(@{@"code":@1,@"data":imageList.firstObject});
        }else{
            self.result(@{@"code":@1,@"data":imageList});
        }
    }
}
@end
